package treqs;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ReqQuery {
//	Statement statement;
	String query;
	ResultSet result;
	public ReqQuery ()
	{
//		statement = null;
		query = null;
		result = null;
	}

	public ReqQuery (treqsConnection tcon, int id)
	{
		query = "Select * from requirements where id=" + Integer.toString(id) + ";";
		System.out.println(query);
		try
		{
			result=getStatement(tcon).executeQuery(query);
		}
		
		catch (SQLException e)
		{
			System.out.println("Caught SQL exception here");
			e.printStackTrace();
		}
	}

	public ReqQuery (treqsConnection tcon)
	{
		query = "Select * from requirements;";
		System.out.println(query);
		try
		{
			result=getStatement(tcon).executeQuery(query);
		}
		
		catch (SQLException e)
		{
			System.out.println("Caught SQL exception here");
			e.printStackTrace();
		}
	}

	public String getResult()
	{
		String uid = null;
		String name = null;
		String desc = null;
		String rat = null;
		String comm = null;
		
		if (result == null)
		{
			return "Query failure";
		}

		try {
			result.next();
			uid = result.getString("uid");
			name = result.getString("name");
			desc = result.getString("description");
			rat = result.getString("rationale");
			comm = result.getString("comment");
		}
		
		catch (SQLException e)
		{
			System.out.println("Caught SQL exception here too");
			e.printStackTrace();
			return "Another query failure";
		}
		
		return uid + " " + name + " " + desc + " " + rat + " " + comm;
	}

	public String getTableHeaderRow()
	{
		String uid = "UID";
		String name = "Name";
		String desc = "Description";
		String rat = "Rationale";
		String comm = "Comment";
		
			uid = "<td> " + uid + " </td>";
			name = "<td> " + name + " </td>";
			desc = "<td> " + desc + " </td>";
			rat = "<td> " + rat + " </td>";
			comm = "<td> " + comm + " </td>";
			
		return "<tr>" + uid + name + desc + rat + comm + "</tr>";
	}

	public String getNextTableRow()
	{
		String uid = null;
		String name = null;
		String desc = null;
		String rat = null;
		String comm = null;
		
		if (result == null)
		{
			return "Query failure";
		}

		try {
			if (result.next())
			{
			uid = "<td> " + result.getString("uid") + " </td>";
			name = "<td> " + result.getString("name") + " </td>";
			desc = "<td> " + result.getString("description") + " </td>";
			rat = "<td> " + result.getString("rationale") + " </td>";
			comm = "<td> " + result.getString("comment") + " </td>";
			}
			else
			{
				return null;
			}
		}
		
		catch (SQLException e)
		{
			System.out.println("Caught SQL exception here too");
			e.printStackTrace();
			return null;
		}
		
		return "<tr>" + uid + name + desc + rat + comm + "</tr>";
	}

	private Statement getStatement(treqsConnection tcon)
	{
		Statement statement = null;
		try
		{
			statement=tcon.connection.createStatement();
		}

		catch (SQLException e)
		{
			e.printStackTrace();
		}
		
		return statement;
	}
}
